package com.permission.controller;

import com.permission.entity.Response;
import com.permission.entity.User;
import com.permission.service.IUserService;
import com.wang.entity.MyPage;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * 处理用户请求的控制器
 *
 * @author my
 * @date 2020/8/4 18:41
 */
@Controller
@RequestMapping("/user")
public class UserController {

    private Logger logger=LoggerFactory.getLogger(this.getClass().getName());
    @Autowired
    private IUserService userService;

    @GetMapping("/")
    public String getUserList(Model model){
        return getUserList(1,10,model);
    }
    @GetMapping("/{page}")
    public String getUserList(@PathVariable Integer page,Model model){
        return getUserList(page,10,model);
    }
    /**
     * 根据当前页码和页面大小查询用户列表
     *
     * @className UserController
     * @author my
     * @date 2020/8/4 20:40
     * @param page 当前页码
     * @param size 每页的大小
     * @return com.permission.entity.Response
     */
    @GetMapping("/{page}/{size}")
    public String getUserList(@PathVariable Integer page, @PathVariable Integer size, Model model){

        logger.info("getUserList");

        return getUserList(page, size, null,model);
    }

    /**
     * 根据用户的状态进行筛选
     *
     * @className UserController
     * @author my
     * @date 2020/8/4 20:40
     * @param page
     * @param size
     * @param status 状态
     * @return com.permission.entity.Response
     */
    @GetMapping("/{page}/{size}/{status}")
    public String getUserList(@PathVariable Integer page,@PathVariable Integer size,@PathVariable Integer status,Model model){
        MyPage<User> myPage=getList(null,page,size);
        if (myPage==null){
            logger.warn("getUserList()---> 获取数据失败");
        }
        model.addAttribute("page",myPage);
        return "user/userList";
    }
    public MyPage<User> getList(Integer status,Integer page ,Integer size){

        if (page==null||page<0){
            logger.warn("getList()--->page is "+page);
            page=1;
        }
        if (size==null||size<0){
            logger.warn("getList()--->size is "+size);
            size=10;
        }

        logger.info("getList()---> status:"+status);
        Map<String, Object> where=null;
        if (status!=null&&status>0){
            where=new HashMap<>();
            where.put("status",status);
        }
       return userService.queryList(page, size, where);
    }

    /**
     * 添加用户
     *
     * @className UserController
     * @author my
     * @date 2020/8/4 20:44
     * @param user 要添加的用户信息
     * @return com.permission.entity.Response
     */
    @PostMapping("/")
    @ResponseBody
    public Response addUser(User user){
        Response response=new Response();

        if (user==null){
            logger.warn("addUser()---> user is null");
            return response;
        }
        logger.info(user.toString());

        if (user.getCreateUser()==null){
            user.setCreateUser(1);
        }


        if (user.getUpdateUser()==null){
            user.setUpdateUser(1);
        }


        int add = userService.add(user);
        if (add>0){
            response.setCode(200);
        }
        return response;
    }


}
